// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using Mono.Unix;
using NLog;
using HAKGERSoft;

namespace wxGadu
{
	public class ImageManager
	{
		#region image names
		public static readonly string EMPTY = "empty.png";
		public static readonly string NOBODY = "nobody.png";
		public static readonly string APP_LOGO = "app-logo.png";
		public static readonly string GADU_SETTINGS = "gadu-settings.png";
		public static readonly string APPEARANCE_SETTINGS = "appearance-settings.png";
		public static readonly string BEHAVIOUR_SETTINGS = "behaviour-settings.png";
		public static readonly string CONTACT_INFO = "contact-info.png";
		public static readonly string DELETE_CONTACT = "delete-contact.png";
		public static readonly string EXIT = "exit.png";
		public static readonly string HISTORY = "history.png";
		public static readonly string HISTORY_CHAT = "history-chat.png";
		public static readonly string INCOMING_CHAT = "incoming-chat.png";
		public static readonly string LOGIN_PASSWORD = "login-password.png";
		public static readonly string NEW_CHAT = "new-chat.png";
		public static readonly string NEW_CONTACT = "new-contact.png";
		public static readonly string SETTINGS = "settings.png";
		public static readonly string MONO = "mono.png";
		public static readonly string WXWIDGETS = "wx-widgets.png";
		public static readonly string CLEAR = "clear.png";
		public static readonly string ENTER = "enter.png";
		public static readonly string SEARCH = "search.png";
		public static readonly string SAVE_AS = "save-as.png";
		public static readonly string STATUS_TRACKING = "status-tracking.png";
		public static readonly string NEW = "new.png";
		public static readonly string NEW_BIG = "new-big.png";
		public static readonly string MAIL = "mail.png";
		public static readonly string MAIL_BIG = "mail-big.png";
		public static readonly string PASSWORD = "password.png";
		public static readonly string PASSWORD_BIG = "password-big.png";
		public static readonly string DELETE_BIG = "delete-big.png";
		public static readonly string COPY= "copy.png";
		#endregion
		
		private wx.ImageList statusImages;
		private Dictionary<int, wx.Bitmap> statusBitmaps;
		private Dictionary<string, wx.Bitmap> bitmaps;
#if LOG
		// logging is initialized after image manager so we cant use nlog here
//		private Logger logger = LogManager.GetCurrentClassLogger();
#endif 
		private string[] statusImageNames = {"gadu-gadu-online.png",
																"gadu-gadu-online-descr.png",
																"gadu-gadu-away.png",
																"gadu-gadu-away-descr.png",
																"gadu-gadu-invisible.png",
																"gadu-gadu-invisible-descr.png",
																"gadu-gadu-offline.png",
																"gadu-gadu-offline-descr.png"};
		
		public ImageManager()
		{
			#region init status bitmaps
			statusImages = new wx.ImageList(16, 16, true);
			statusBitmaps = new Dictionary<int, wx.Bitmap>();
			
			for (int i = 0; i < statusImageNames.Length; i++)
			{
				wx.Bitmap tmpBmp = GetBitmapFromAssembly(statusImageNames[i]);
				statusImages.Add(tmpBmp);
				statusBitmaps.Add(i, tmpBmp);
			}
			#endregion
			
			#region init other bitmaps
			bitmaps = new Dictionary<string, wx.Bitmap>();
			bitmaps.Add(EMPTY, GetBitmapFromAssembly(EMPTY));
			bitmaps.Add(NOBODY, GetBitmapFromAssembly(NOBODY));	
			bitmaps.Add(APP_LOGO, GetBitmapFromAssembly(APP_LOGO));	
			bitmaps.Add(GADU_SETTINGS, GetBitmapFromAssembly(GADU_SETTINGS));	
			bitmaps.Add(APPEARANCE_SETTINGS, GetBitmapFromAssembly(APPEARANCE_SETTINGS));	
			bitmaps.Add(BEHAVIOUR_SETTINGS, GetBitmapFromAssembly(BEHAVIOUR_SETTINGS));	
			bitmaps.Add(CONTACT_INFO, GetBitmapFromAssembly(CONTACT_INFO));	
			bitmaps.Add(DELETE_CONTACT, GetBitmapFromAssembly(DELETE_CONTACT));	
			bitmaps.Add(EXIT, GetBitmapFromAssembly(EXIT));	
			bitmaps.Add(HISTORY, GetBitmapFromAssembly(HISTORY));	
			bitmaps.Add(HISTORY_CHAT, GetBitmapFromAssembly(HISTORY_CHAT));	
			bitmaps.Add(INCOMING_CHAT, GetBitmapFromAssembly(INCOMING_CHAT));	
			bitmaps.Add(LOGIN_PASSWORD, GetBitmapFromAssembly(LOGIN_PASSWORD));	
			bitmaps.Add(NEW_CHAT, GetBitmapFromAssembly(NEW_CHAT));	
			bitmaps.Add(NEW_CONTACT, GetBitmapFromAssembly(NEW_CONTACT));	
			bitmaps.Add(SETTINGS, GetBitmapFromAssembly(SETTINGS));	
			bitmaps.Add(MONO, GetBitmapFromAssembly(MONO));	
			bitmaps.Add(WXWIDGETS, GetBitmapFromAssembly(WXWIDGETS));	
			bitmaps.Add(CLEAR, GetBitmapFromAssembly(CLEAR));	
			bitmaps.Add(SEARCH, GetBitmapFromAssembly(SEARCH));	
			bitmaps.Add(ENTER, GetBitmapFromAssembly(ENTER));	
			bitmaps.Add(SAVE_AS, GetBitmapFromAssembly(SAVE_AS));
			bitmaps.Add(STATUS_TRACKING, GetBitmapFromAssembly(STATUS_TRACKING));
			bitmaps.Add(NEW, GetBitmapFromAssembly(NEW));
			bitmaps.Add(NEW_BIG, GetBitmapFromAssembly(NEW_BIG));
			bitmaps.Add(MAIL, GetBitmapFromAssembly(MAIL));
			bitmaps.Add(MAIL_BIG, GetBitmapFromAssembly(MAIL_BIG));
			bitmaps.Add(PASSWORD, GetBitmapFromAssembly(PASSWORD));
			bitmaps.Add(PASSWORD_BIG, GetBitmapFromAssembly(PASSWORD_BIG));
			bitmaps.Add(DELETE_BIG, GetBitmapFromAssembly(DELETE_BIG));
			bitmaps.Add(COPY, GetBitmapFromAssembly(COPY));
			#endregion
			
		}
		
		private wx.Image GetImageFromAssembly(string fileName)
		{
			byte[] byteImg;
			try
			{
				byteImg = this.GetBytesFromAssembly(fileName, "Resources");
			}
			catch (MissingManifestResourceException e)
			{
#if LOG
//				logger.ErrorException(String.Format(Catalog.GetString("Failed to load file: {0} from embeded resources file"), fileName), e);
				System.Console.WriteLine(String.Format(Catalog.GetString("Failed to load file: {0} from embeded resources file.\n{1}"), fileName, e.Message));
#endif
				// return empty image
				return new wx.Image();
			}
			
			wx.Image img = new wx.Image(byteImg, wx.BitmapType.wxBITMAP_TYPE_PNG);
			
			return img;
		}
		
		public static int ImageIndexFromStatus(GGStatusType status, string description)
		{
			switch (status)
			{
				case GGStatusType.Available:
					if (description == String.Empty)
						return StatusConstans.online;
					else
						return StatusConstans.onlineDescr;
										
				case GGStatusType.Busy:
					if (description == String.Empty)
					    return StatusConstans.away;
					else
					    return StatusConstans.awayDescr;
				    
			   	case GGStatusType.Invisible:
			    	if (description == String.Empty)
			        	return StatusConstans.invisible;
			        else
			        	return StatusConstans.invisibleDescr;

			    case GGStatusType.NotAvailable:
			        if (description == String.Empty)
			            return StatusConstans.offline;
			        else
			            return StatusConstans.offlineDescr;
			
				default: return -1;
			}
		}
		
		private wx.Bitmap GetBitmapFromAssembly(string fileName)
		{
			wx.Bitmap bmp = new wx.Bitmap(GetImageFromAssembly(fileName));
			
			return bmp;
		}
		
		/// <summary>
		/// Reads image data from embeded resources file
		/// </summary>
		/// <param name="name">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="baseName">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Byte"/>
		/// </returns>
		/// <remarks>
		/// Image data is stored in resources file as <c>byte</c>
		/// arrays.
		/// </remarks>
		private byte[] GetBytesFromAssembly(string name, string baseName)	
		{
			ResourceManager rm = new ResourceManager(baseName, Assembly.GetExecutingAssembly());
			
			return (byte[]) rm.GetObject(name);
		}
		
		//// <value>
		/// Collection of images used to represent GG status
		/// </value>
		public wx.ImageList StatusImages
		{
			get { return statusImages; }
		}
		
		//// <value>
		/// Collection of images used to represent GG status
		/// </value>
		/// <remarks>
		/// This collection keeps the same data that 
		/// <c>StatusImages</c> but another list is needed 
		/// because wx.Net wx.ImageList doesn't expose GetBitmap method.
		/// </remarks>
		public Dictionary<int, wx.Bitmap> StatusBitmaps
		{
			get { return statusBitmaps; }
		}
		
		//// <value>
		/// Bitmap coresponding to given <c>imageName</c>
		/// </value>
		/// <remarks>
		/// <c>imageName</c> value must be one of the image names 
		/// readonly string in <c>ImageManager</c>
		/// </remarks>
		public wx.Bitmap this[string imageName]
		{
			get { return bitmaps[imageName]; }
		}
		
	}
}
